//
//  UIPageControlController.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/1/6.
//

import Foundation
import UIKit
//页码控制器
class UIPageControlController:UIViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        
        let mUIPageControl = UIPageControl(frame: CGRect(x: 20, y: 100, width: 280, height: 30))
        
        mUIPageControl.numberOfPages = 10
        mUIPageControl.backgroundColor = UIColor.red
        mUIPageControl.pageIndicatorTintColor = UIColor.green
        mUIPageControl.currentPageIndicatorTintColor = UIColor.blue
        mUIPageControl.currentPage = 3
        mUIPageControl.addTarget(self, action: #selector(change), for: UIControl.Event.valueChanged)
        
        self.view.addSubview( mUIPageControl )
        
        //分部控制器UISegmentedControl
        let mUISegmentedControl = UISegmentedControl(items:["基础设置","高级设置","设备设置"])
        //尺寸
        mUISegmentedControl.frame = CGRect(x: 0, y: 200, width: 150, height: 60)
        //控件风格颜色
        mUISegmentedControl.tintColor = UIColor.blue //主键风格颜色
        //mUISegmentedControl.isMomentary = true  //设置true 就不选中颜色 放开则恢复原样   false 点击就选中，并高亮
        mUISegmentedControl.selectedSegmentTintColor = UIColor.red //选中颜色
        mUISegmentedControl.addTarget(self, action: #selector(change2), for: UIControl.Event.valueChanged)
        //添加当前试图
        self.view.addSubview( mUISegmentedControl )
        
        //分部控制器实现增删改查  动态新增
        
        let mUISegmentedControl2 = UISegmentedControl(items:["基础","高级","设备"])
        //尺寸
        mUISegmentedControl2.frame = CGRect(x: 0, y: 300, width: 200, height: 60)
        //控件风格颜色
        mUISegmentedControl2.tintColor = UIColor.blue //主键风格颜色
        //mUISegmentedControl.isMomentary = true  //设置true 就不选中颜色 放开则恢复原样   false 点击就选中，并高亮
        mUISegmentedControl2.selectedSegmentTintColor = UIColor.red //选中颜色
        mUISegmentedControl2.addTarget(self, action: #selector(change2), for: UIControl.Event.valueChanged)
        mUISegmentedControl2.insertSegment(withTitle: "删除", at: 0, animated: true)
        
        //添加图片
        mUISegmentedControl2.insertSegment(with: UIImage(named: "button")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), at: 4, animated: true)
        //删除一个item
        
        //mUISegmentedControl2.removeAllSegments()
        //删除第一个
        mUISegmentedControl2.removeSegment(at: 0, animated:true)
        //修改某个item的图片
        mUISegmentedControl2.setImage(UIImage(named: "button")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal),forSegmentAt: 0 )
        //修改标题
        
        mUISegmentedControl2.setTitle("你好", forSegmentAt: 1)
        
        //设置宽度
        mUISegmentedControl2.setWidth(100, forSegmentAt: 0)
        
        //宽度自适应
        mUISegmentedControl2.apportionsSegmentWidthsByContent = true
        
        //添加当前试图
        self.view.addSubview( mUISegmentedControl2 )
        
        
    }
    @objc func change(_ mUIPageControl:UIPageControl){
        print( "当前页码：",mUIPageControl.currentPage )
    }
    
    @objc func change2(_ mUISegmentedControl:UISegmentedControl){
        print( "当前页码：",mUISegmentedControl.selectedSegmentIndex )
    }
    
}
